﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZedGraph;
using System.Collections;
using TSTreeThesis.Utilities;
using System.IO;

namespace TSTreeThesis.UI
{
    public partial class LoadQuery : Form
    {
        public LoadQuery()
        {
            InitializeComponent();
        }

        private string fileName;
        private double[] query;
        private double[] encodedQuery;
        private PointPairList list;

        private double mean;
        private double std;

        public LoadQuery(string fileName, double mean, double std)
        {
            InitializeComponent();
            this.fileName = fileName;
            this.mean = mean;
            this.std = std;
        }

        public PointPairList GetQueryList()
        {
            return list;
        }

        public double[] GetQueryArr()
        {
            return query;
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            int curLine = 0;
            ArrayList tempData = new ArrayList();
            int fromIndex = 0;
            int toIndex = Utility.SLIDING_WINDOW_SIZE_ORIGINAL;
            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {

                    StreamReader reader = new StreamReader(fs);
                    String line;
                    int count = 0;
                    fs.Position = 0;
                    while ((line = reader.ReadLine()) != null && curLine < toIndex)
                    {
                        if (curLine >= fromIndex && curLine < toIndex)
                        {
                            tempData.Add((Convert.ToDouble(line) - mean) / std);
                            count++;
                        }
                        curLine++;
                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            query = new double[Utility.SLIDING_WINDOW_SIZE_ORIGINAL];
            tempData.CopyTo(query);

            PAATransform transform = new PAATransform();
            encodedQuery = transform.Transform(Utility.COMPRESSION_RATE, query);

            //Copy to PoitPairList
            list = new PointPairList();
            for (int i = 0; i < query.Length; i++)
            {
                double p = query[i];
                list.Add(i, p);
            }
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Close();
        }

        private void LoadQuery_Load(object sender, EventArgs e)
        {
            backgroundWorker.RunWorkerAsync();
        }
    }
}
